/*
:获得文件行数的方法
 有 bufferedReader.readLine 方法 或者查找byte是否=='\n',比较谁快！
 */
package day9.Exer;

import java.io.*;


public class Exer7 {
    public static void main(String[] args) {
        BufferedReader brd=null;
        FileInputStream fis=null;
        try {
            long start1=System.nanoTime();
            brd=new BufferedReader(new FileReader("E:\\作业\\b.txt"));
            int count=0;
            String str;
            while((str=brd.readLine())!=null){
                count++;
            }
            long end1=System.nanoTime();
            long time1=end1-start1;
            System.out.println("一共"+count+"行，bufferedReader.readLine 方法用时"+time1+"纳秒");
            long start2=System.nanoTime();
            fis=new FileInputStream("E:\\作业\\b.txt");
            int count1=0;
            byte[]b=new byte[10];
            while(fis.read(b)>-1){
                fis.read(b);
                for(byte a:b){
                    if((char)a=='\n'){
                        count1++;
                    }
                }
            }
            long end2=System.nanoTime();
            long time2=end2-start2;
            System.out.println("一共"+count1+"行，查找byte方法用时"+time2+"纳秒");


        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if(brd!=null){
                try {
                    brd.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(fis!=null){
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
